package com.jumi.microservice.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jm.member.common.core.utils.ServletUtils;
import com.jm.member.common.core.web.page.TableDataInfo;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.domain.AppMessageAll;
import com.jumi.microservice.dto.request.NoticeQueryRequest;
import com.jumi.microservice.dto.request.NoticeUpdateRequest;
import com.jumi.microservice.entity.JmNotice;
import com.jumi.microservice.enums.CommonEnum;
import com.jumi.microservice.enums.SendType;
import com.jumi.microservice.service.IJmNoticeService;
import com.jumi.microservice.service.IJmUserService;
import com.jumi.microservice.service.IMessageService;
import com.jumi.microservice.service.PageService;
import com.jumi.microservice.util.WrapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * <p>
 * 推送消息表 前端控制器
 * </p>
 *
 * @author 要挑客户管理团队
 * @since 2020-08-06
 */
@RestController
@RequestMapping("/jm-notice")
@Api(value = "会员通知", tags = {"会员通知接口"})
public class JmNoticeController {

    @Autowired
    IJmNoticeService noticeService;

    @Autowired
    IJmUserService userService;

    @DubboReference(check = false)
    IMessageService messageService;

    @PostMapping("/getDataInfo")
    @ApiOperation("查询会员通知分页")
    public TableDataInfo<JmNotice> getDataInfo(NoticeQueryRequest request, Integer size, Integer current) {
        Page page = new Page(current, size);
        QueryWrapper wrapper = WrapperUtil.getWrapper(null, request);
        wrapper.orderByDesc("create_time");
        page = noticeService.getPage(page, wrapper);
        return PageService.getDataTableInfo(page);
    }

    @PostMapping("/sendNotice")
    @ApiOperation("添加消息通知")
    @Transactional
    public ResponseResult sendNotice(@Validated NoticeUpdateRequest updateRequest) {
        Long adminId = ServletUtils.getLoginId();
        String adminName = ServletUtils.getLoginName();
        int userCount;
        String receive_id = "";
        SendType sendType = updateRequest.getSendType();
        if (!sendType.equals(SendType.全部)) {
            return ResponseResult.error("只可发送全部会员类型！");
          /*  QueryWrapper wrapper=new QueryWrapper();

            if(sendType.equals(SendType.指定会员)){
                wrapper.in("id",updateRequest.getIds());
            }else{
                Integer[] levels = updateRequest.getLevels();
                wrapper.in("level",levels);
            }
            wrapper.eq("allow_login",1);
            wrapper.select("id");
            List list = userService.listObjs(wrapper);
            userCount=list.size();
            receive_id=list.toString().replaceAll("\\[", "").replaceAll("\\]","");*/
        } else {
            userCount = userService.getUserCountByNormal();
        }
        JmNotice jmNotice = new JmNotice();
        BeanUtils.copyProperties(updateRequest, jmNotice);
        if (userCount != 0) {
            jmNotice.setReadNum(0);
            jmNotice.setSendNum(userCount);
            jmNotice.setStatus(CommonEnum.是);
            jmNotice.setReceiveId(receive_id);
            jmNotice.setCreateTime(LocalDateTime.now());
            jmNotice.setAdminId(adminId);
            jmNotice.setAdminName(adminName);
            boolean local = noticeService.save(jmNotice);
            AppMessageAll appMessageAll = new AppMessageAll();
            boolean push = false;
            try {
                appMessageAll.setMessageId(jmNotice.getId());
                appMessageAll.setTitle(updateRequest.getTitle());
                appMessageAll.setMessageContent(updateRequest.getContent());
                Integer spuId=updateRequest.getSpuId()==null?0:updateRequest.getSpuId();
                appMessageAll.setSpuId(spuId);
                push = messageService.messageAllSend(appMessageAll);
            } catch (Exception e) {
                return ResponseResult.error("推送失败!"+e.getMessage());
            }
            if(local&&push)
                return ResponseResult.success("推送成功!");
            else
                return ResponseResult.error(StrUtil.format("本地通知结果:{},推送通知结果{}",local,push).toString());
        }

        return ResponseResult.error("无可发送会员！");
    }
}
